package RenderLayer;

import ModelLayer.Menu;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;

import com.example.genericrpg_android.GameView;

public class TitleView extends View {
	Menu mMenu;
	private Bitmap tImg;
	private Bitmap menuArrow, menuArrowS;
	private int alpha;
	private Rect titleImage, titleTarget, arrowTarget;
	private boolean noTarget = true;
	private int W = 0, H = 0;
	
	public TitleView(Menu menu, Context activity){
		mMenu = menu;
		alpha=255;
		/* Initializes the images for the title screen and menu arrow. */
		try {
			tImg = BitmapFactory.decodeStream(activity.getAssets().open("images/titleScreen.png"));
			menuArrow = BitmapFactory.decodeStream(activity.getAssets().open("images/menuArrow.png"));
			menuArrowS = BitmapFactory.decodeStream(activity.getAssets().open("images/menuArrow_s.png"));
		} catch (Exception e) {
			e.printStackTrace();
		}

		arrowTarget = new Rect(0,0,0,0);
		titleTarget = new Rect(0,0,0,0);
		titleImage = new Rect(0,0,tImg.getWidth(),tImg.getHeight()); 
	}
	
	@Override
	public void render(Canvas g, GameView root) {
		// TODO Auto-generated method stub
		/* Draws the title screen */

		titleImage.set(0,0,tImg.getWidth(),tImg.getHeight());
		
		if(noTarget){
			W = root.getWidth();
			H = root.getHeight();
			noTarget = !noTarget;
		}
		int d;
		if (W < tImg.getWidth()){
			d = (tImg.getWidth() - W)*100/W;
			titleTarget.set(0+d, 0, W-d, H);
			menuArrow = menuArrowS;
		}
		else {
			d = ((W - tImg.getWidth())*200)/tImg.getWidth();
			titleTarget.set(0+d, 0, W-d, H  );
		}
		g.drawARGB(255, 255, 255, 255);
		
		g.drawBitmap(tImg, titleImage, titleTarget,	null);

		/*
		 * Draws the menu arrow in a certain position depending on which menu
		 * item is currently selected.
		 */
		if (mMenu.isSelected()){
			if (alpha == 255)
				alpha = 0;
			if (alpha<=253)
				alpha += 3;
			g.drawARGB(alpha, 0, 0, 0);
		}
		else {	
			mMenu.reset();
			if (mMenu.getOption().equals("Start New Game")) {
				g.drawBitmap(menuArrow, (W / 4), (int) (H *3.1 / 6), null);
			} else if (mMenu.getOption().equals("Load Game")) {
				g.drawBitmap(menuArrow, (W / 4), (int)(H* 3.6/ 6), null);
			} else if (mMenu.getOption().equals("Exit")) {				
				g.drawBitmap(menuArrow, (W / 4), (int)(H* 4.2/ 6), null);
			}
		}
	}
}
